//
//  InvestigativeOrganViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/6/19.
//  Copyright © 2017年 test. All rights reserved.
//

#import "InvestigativeOrganViewController.h"

@interface InvestigativeOrganViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *investigativeTablewView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIButton *barAddButton;


@end

@implementation InvestigativeOrganViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.barTitleLable.text = @"检查机关列表 ";
    self.dataSource = [NSMutableArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self initUI];
    [self getData];
}
- (void)initUI{
    
    [self.view addSubview:self.investigativeTablewView];
    
    [self.view addSubview:self.barAddButton];
}
- (void)getData{
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@glid=%@",PROCURATORATE_URL,self.caseDetailDic[@"id"]] dic:nil Succeed:^(id responseData) {
        self.dataSource = responseData[@"result"];
        if (self.dataSource.count == 0) {
            [MBProgressHUD showSuccess:@"暂无更多数据"];
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"data = %@",self.dataSource);
        [self.investigativeTablewView reloadData];
        [self.investigativeTablewView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
    
}
- (void)loadNewData{
    [self.dataSource removeAllObjects];
    [self getData];
}

#pragma mark - Button Target
- (void)action_AddButton:(UIButton *)sender{
    AddInfoViewController *addVC = [[AddInfoViewController alloc] init];
    addVC.flag = self.log;
    addVC.caseDetailDic = self.caseDetailDic;
    [self.navigationController pushViewController:addVC animated:YES];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InvestigativeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"investigativeCell" forIndexPath:indexPath];
    cell.titleLable.text = self.dataSource[indexPath.row][@"ah_number"];
    cell.caseOrgans.text = [NSString stringWithFormat:@"办案机关： %@",self.dataSource[indexPath.row][@"bajg"]];
    cell.department.text = [NSString stringWithFormat:@"部门： %@",self.dataSource[indexPath.row][@"bm"]];
    cell.undertaker.text = [NSString stringWithFormat:@"承办人： %@",self.dataSource[indexPath.row][@"cbr"]];
    cell.contactNumber.text = [NSString stringWithFormat:@"联系电话： %@",self.dataSource[indexPath.row][@"tel"]];
    cell.prosecutionDate.text = [NSString stringWithFormat:@"审查起诉日期： %@ - %@",self.dataSource[indexPath.row][@"scqs_star"],self.dataSource[indexPath.row][@"scqs_end"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EditInfoViewController *EIVC = [[EditInfoViewController alloc] init];
    EIVC.flag = self.log;
    EIVC.caseDetailDic = self.caseDetailDic;
    EIVC.listDetailDic = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:EIVC animated:YES];
}
#pragma mark - Getter

- (UITableView *)investigativeTablewView{
    if (!_investigativeTablewView) {
        _investigativeTablewView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStyleGrouped];
        _investigativeTablewView.backgroundColor = [UIColor clearColor];
        _investigativeTablewView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _investigativeTablewView.delegate = self;
        _investigativeTablewView.dataSource = self;
        _investigativeTablewView.estimatedRowHeight = 210;
        [_investigativeTablewView registerClass:[InvestigativeCell class] forCellReuseIdentifier:@"investigativeCell"];
        _investigativeTablewView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
    }
    return _investigativeTablewView;
}
- (UIButton *)barAddButton{
    if (!_barAddButton) {
        _barAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _barAddButton.hidden = self.hideOrNot;
        _barAddButton.frame = CGRectMake(335, 30, 25, 25);
        [_barAddButton setBackgroundImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
        [_barAddButton addTarget:self action:@selector(action_AddButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _barAddButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
